//
//  ManagementDetailsViewController.m
//  Againstpoor
//
//  Created by 辰领科技 on 16/11/7.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "ManagementDetailsViewController.h"
#import "ManagementDetailsTableViewCell.h"

static NSString * Identifier = @"tableViewCell";

@interface ManagementDetailsViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIScrollView *scroview;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;
@property (strong, nonatomic) NSMutableArray *array;
@property(nonatomic,strong)NSMutableArray * heightArray;

@end

@implementation ManagementDetailsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self getdetail];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _array = [NSMutableArray array];
    _heightArray = [NSMutableArray array];
    [self initViewLayout];
}
- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroview];
    
    _nameLabel.text = [NSString stringWithFormat:@"驻村干部：%@",_name];
    NSString * number = [NSString stringWithFormat:@"%@",_number];
    NSMutableAttributedString * strOne = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@次",number]];
    [strOne addAttribute:NSForegroundColorAttributeName value:kGetColor(182, 2, 1) range:NSMakeRange(0, number.length)];
    [_numberLabel setAttributedText:strOne];
}

- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark - 走访详情
- (void)getdetail {
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setObject:self.ACCID forKey:@"accId"];
    self.shareApi.delegate = self;
    [self.shareApi  requestOperationWithDic:dic withPath:F_messageCollectionDetailes withHttpType:@"POST" withTag:F_F_messageCollectionDetailes];
}

#pragma mark - <UITableViewDelegate&&UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//定义 UITableView cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

//定义 UITableView cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = [_heightArray[indexPath.row] floatValue]+5*VerticalRatio();
    return height;
}

//定义 UITableView cell 展示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ManagementDetailsTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell= [[ManagementDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    NSDictionary *dic = _array[indexPath.row];
    cell.nameLabel.text = [Common stringFromData:dic[@"name"]];
    cell.timeLabel.text = [Common stringFromData:dic[@"strTime"]];
    cell.addressLabel.text = [Common stringFromData:dic[@"address"]];
    cell.detailLabel.frame = flexibleFrame(CGRectMake(101, 86, 201, 0), NO);
    cell.detailLabel.numberOfLines = 0;
    cell.detailLabel.text = [Common stringFromData:dic[@"details"]];
    [cell.detailLabel sizeToFit];
    
    
    
    NSString * imageStr = [NSString stringWithFormat:@"%@",dic[@"image"]];
    imageStr = [imageStr substringToIndex:imageStr.length];
    NSArray *images = [NSArray array];
    images = [[imageStr componentsSeparatedByString:@","]mutableCopy];
    
    
    CGFloat height = [_heightArray[indexPath.row] floatValue];
    cell.detailTitle.frame = flexibleFrame(CGRectMake(13, height-74, 122, 18), NO);

    for (int i = 0; i<images.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:flexibleFrame(CGRectMake(101+i*70, height-74, 65, 65), NO)];
        NSString *image = [Common stringFromData:images[i]];
        [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
        
        [cell addSubview:imageView];
    }
    
    UILabel * line =[[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0, height-5, 320, 5), NO)];
    line.backgroundColor = kGetColor(243, 243, 243);
    [cell addSubview:line];
    
    return cell;

}

//定义 UITableView cell 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomePageStoryboard" bundle:nil];
    //    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"AlertsDetail"];
    //    [[Common getNavigation]pushViewController:page];
}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{

    if (tag==F_F_messageCollectionDetailes) {
        _array = database[@"data"];

        for (int i = 0; i<_array.count; i++) {
            CGFloat height = 86+14+70;
            UILabel * label = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 201, 0), NO)];
            label.numberOfLines = 0;
            label.text = [Common stringFromData:_array[i][@"details"]];
            [label sizeToFit];
            CGSize size = [label.text sizeWithFont:[UIFont systemFontOfSize:15*VerticalTexeRatio()] maxSize:flexibleSize(CGSizeMake(201, 150), NO)];
            height += size.height;
            [_heightArray addObject:@(height)];
        }
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView reloadData];
        }else  {
            
        }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    

    [self dismissWaitDialog];
    
    
}

@end
